﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Specialized;
using System.Configuration;
using System.Net.Mail;
using System.Net;
using System.Web;

namespace TQB.Common.EmailHelpers
{
    #region EmailSettings

    public class EmailSettings
    {
        private static EmailSettings m_Instance = new EmailSettings();
        private NameValueCollection m_MailSection;

        private string m_SmtpServer;
        private int? m_SmtpPort;
        private string m_UserName;
        private string m_Password;

        private string m_From;
        private string m_To;
        private string m_Cc;
        private string m_Bcc;

        private string m_MailFormat;
        private string m_Priority;
        private string m_BodyEncoding;
        private string m_Subject;
        private string m_BodyTemplate;
        private string m_AdvisorInfoChangeReportTemplate;

        private bool? m_EnableSsl;
        private bool? m_UseDefaultCredentials;


        public static EmailSettings Instance
        {
            get
            {
                return m_Instance;
            }
        }

        private EmailSettings()
        {
            m_MailSection = ConfigurationManager.GetSection("CSEmailSettings") as NameValueCollection;
        }

        public string SmtpServer
        {
            get
            {
                if (string.IsNullOrEmpty(m_SmtpServer))
                {
                    m_SmtpServer = m_MailSection["SmtpServer"];
                }
                return m_SmtpServer;
            }
        }

        public int SmtpPort
        {
            get
            {
                if (!m_SmtpPort.HasValue)
                {
                    int port = 25;
                    int.TryParse(m_MailSection["SmtpPort"], out port);
                    m_SmtpPort = port;
                }
                return m_SmtpPort.Value;
            }
        }

        public string UserName
        {
            get
            {
                if (string.IsNullOrEmpty(m_UserName))
                {
                    m_UserName = m_MailSection["UserName"];
                }
                return m_UserName;
            }
        }

        public string Password
        {
            get
            {
                if (string.IsNullOrEmpty(m_Password))
                {
                    m_Password = m_MailSection["Password"];
                }
                return m_Password;
            }
        }

        public string From
        {
            get
            {
                if (string.IsNullOrEmpty(m_From))
                {
                    m_From = m_MailSection["From"];
                }
                return m_From;
            }
        }

        public string To
        {
            get
            {
                if (string.IsNullOrEmpty(m_To))
                {
                    m_To = m_MailSection["To"];
                }
                return m_To;
            }
        }

        public string Cc
        {
            get
            {
                if (string.IsNullOrEmpty(m_Cc))
                {
                    m_Cc = m_MailSection["Cc"];
                }
                return m_Cc;
            }
        }

        public string Bcc
        {
            get
            {
                if (string.IsNullOrEmpty(m_Bcc))
                {
                    m_Bcc = m_MailSection["Bcc"];
                }
                return m_Bcc;
            }
        }

        public string MailFormat
        {
            get
            {
                if (string.IsNullOrEmpty(m_MailFormat))
                {
                    m_MailFormat = m_MailSection["MailFormat"];
                }
                return m_MailFormat;
            }
        }

        public string Priority
        {
            get
            {
                if (string.IsNullOrEmpty(m_Priority))
                {
                    m_Priority = m_MailSection["Priority"];
                }
                return m_Priority;
            }
        }

        public string BodyEncoding
        {
            get
            {
                if (string.IsNullOrEmpty(m_BodyEncoding))
                {
                    m_BodyEncoding = m_MailSection["BodyEncoding"];
                }
                return m_BodyEncoding;
            }
        }

        public string Subject
        {
            get
            {
                if (string.IsNullOrEmpty(m_Subject))
                {
                    m_Subject = m_MailSection["SubjectPrefix"] + m_MailSection["Subject"];
                }
                return m_Subject;
            }
        }

        public string BodyTemplate
        {
            get
            {
                if (string.IsNullOrEmpty(m_BodyTemplate))
                {
                    m_BodyTemplate = m_MailSection["BodyTemplate"];
                }
                return m_BodyTemplate;
            }
        }

        public bool EnableSsl
        {
            get
            {
                if (!m_EnableSsl.HasValue)
                {
                    bool ret = false;
                    bool.TryParse(m_MailSection["EnableSsl"], out ret);
                    m_EnableSsl = ret;
                }

                return m_EnableSsl.Value;
            }
        }

        public bool UseDefaultCredentials
        {
            get
            {
                if (!m_UseDefaultCredentials.HasValue)
                {
                    bool ret = false;
                    bool.TryParse(m_MailSection["UseDefaultCredentials"], out ret);
                    m_UseDefaultCredentials = ret;
                }

                return m_UseDefaultCredentials.Value;
            }
        }

    }

    #endregion

    public class FAMEMailer
    {
        private static object look_obj = new object();

        private System.Net.Mail.SmtpClient m_SmtpClient;

        public FAMEMailer() { }

        public bool SendMail(System.Net.Mail.MailMessage message)
        {
            bool retValue = false;

            if (message != null)
            {
                lock (look_obj)
                {
                    SmtpClient.Send(message);
                }
                retValue = true;
            }

            return retValue;
        }

        public System.Net.Mail.SmtpClient SmtpClient
        {
            get
            {
                if (m_SmtpClient == null)
                {
                    m_SmtpClient = new System.Net.Mail.SmtpClient(EmailSettings.Instance.SmtpServer, EmailSettings.Instance.SmtpPort);
                    m_SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    m_SmtpClient.EnableSsl = EmailSettings.Instance.EnableSsl;

                    m_SmtpClient.UseDefaultCredentials = EmailSettings.Instance.UseDefaultCredentials;

                    if (!m_SmtpClient.UseDefaultCredentials)
                    {
                        m_SmtpClient.Credentials = new NetworkCredential(EmailSettings.Instance.UserName, EmailSettings.Instance.Password);
                    }
                }

                return m_SmtpClient;
            }


        }

    }

    public class MailService
    {
        private static MailService m_Instance = new MailService();
        private FAMEMailer m_Mailer;

        private MailService()
        {
            m_Mailer = new FAMEMailer();
        }

        public static MailService Instance
        {
            get
            {
                return m_Instance;
            }
        }

        public bool SendMail(System.Net.Mail.MailMessage message)
        {
            string errorMessage = null;
            return this.SendMail(message, ref errorMessage);
        }

        public bool SendMail(System.Net.Mail.MailMessage message, ref string errorMessage)
        {
            try
            {
                bool retVal = m_Mailer.SendMail(message);
                return retVal;
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }

            return false;
        }
    }


    public class MailTemplate
    {
        private static MailTemplate m_MailTemplate = new MailTemplate();

        private MailTemplate() { }

        public static MailTemplate Instance
        {
            get
            {
                return m_MailTemplate;
            }
        }

        private System.Net.Mail.MailMessage GetMessage(string xml)
        {
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath("~/MailTemplate/" + xml));

            System.Xml.XmlNodeList from = doc.GetElementsByTagName("From");
            if (from.Count > 0) { message.From = new System.Net.Mail.MailAddress(from[0].InnerText); }

            System.Xml.XmlNodeList to = doc.GetElementsByTagName("To");
            if (to.Count > 0)
            {
                foreach (string t in to[0].InnerText.Split(';'))
                {
                    message.To.Add(t);
                }
            }

            System.Xml.XmlNodeList cc = doc.GetElementsByTagName("Cc");
            if (cc.Count > 0)
            {
                foreach (string c in cc[0].InnerText.Split(';'))
                {
                    message.To.Add(c);
                }
            }

            System.Xml.XmlNodeList subject = doc.GetElementsByTagName("Subject");
            if (subject.Count > 0) { message.Subject = subject[0].InnerText; }

            System.Xml.XmlNodeList contents = doc.GetElementsByTagName("Contents");
            if (contents.Count > 0) { message.Body = contents[0].InnerText; }

            return message;
        }


        /// <summary>
        /// Template of email to be sent from Phillip Mutual Bhd to upline, ie FC/SFC/GFC/ /Team Manager/Firm.
        /// </summary>
        public System.Net.Mail.MailMessage testEmail
        {
            get
            {
                return GetMessage("testEmail.xml");
            }
        }
    }


}
